<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>demo-file</title>
    <style>
      .opt-item {
        padding: 10px 0;
      }
      .opt-item img {
        width: 100%;
      }
      .file-area {
        width: 200px;
        height: 200px;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <h2>文件操作</h2>
    <div class="opt-item">
      <button id="openBtn">打开文件</button>
    </div>
    <div class="opt-item">
      <button id="saveBtn">保存文件</button>
    </div>
    <div class="opt-item">
      <button id="showMsgBtn">显示消息</button>
    </div>
    <div class="opt-item">
      <button id="showNoticeBtn">底部通知栏消息</button>
    </div>
    <div class="opt-item">
      <img src="" alt="" />
    </div>
    <div class="file-area">这里放文件</div>
    <script>
      const { dialog } = require('electron').remote
      const fs = require('fs')

      const domOpenBtn = document.querySelector('#openBtn')
      const domSaveBtn = document.querySelector('#saveBtn')
      const domShowMsgBtn = document.querySelector('#showMsgBtn')
      const domShowNoticeBtn = document.querySelector('#showNoticeBtn')
      const fileArea = document.querySelector('.file-area')

      console.log(fileArea)

      fileArea.addEventListener('drop', (e) => {
        e.preventDefault() //阻止e的默认行为
        console.log(e)
        // const files = e.dataTransfer.files
        // if (files && files.length >= 1) {
        //   const path = files[0].path
        //   console.log('file:', path)
        //   const content = fs.readFileSync(path)
        //   console.log(content.toString())
        // }
      })

      const domImg = document.querySelector('.opt-item img')

      domOpenBtn.onclick = function () {
        dialog
          .showOpenDialog({
            title: '请选择文件',
            defaultPath: 'D:\\Media',
            filters: [
              {
                name: 'img',
                extensions: ['jpg', 'png', 'gif'],
              },
            ],
            buttonLabel: '选择图片',
          })
          .then((data) => {
            console.log(data)
            domImg.setAttribute('src', data.filePaths[0])
          })
          .catch((err) => {
            console.log(err)
          })
      }

      domSaveBtn.onclick = function () {
        dialog
          .showSaveDialog({
            title: '保存文件',
          })
          .then((result) => {
            fs.writeFileSync(result.filePath, 'some words')
            console.log(result)
          })
          .catch((err) => {
            console.log(err)
          })
      }

      domShowMsgBtn.onclick = function () {
        dialog
          .showMessageBox({
            // type 取值 none,info,error,question
            type: 'info',
            title: '演示消息',
            message: '这是演示消息',
            buttons: ['btn1', 'btn2'],
          })
          .then((result) => {
            console.log(result)
          })
      }

      const option = {
        title: '底部通知消息',
        body: '消息内容，消息内容，消息内容，消息内容',
      }
      domShowNoticeBtn.onclick = function () {
        new window.Notification(option.title, option)
      }
    </script>
  </body>
</html>
